/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.bopet.jocadv.gui;

import com.jogamp.opengl.util.FPSAnimator;
import eu.bopet.jocadv.core.Component;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.awt.GLJPanel;
import javax.swing.JPopupMenu;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.nodes.Node;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//eu.bopet.jocadv.gui//Component//EN",
        autostore = false
)
@TopComponent.Description(
        preferredID = "ComponentTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE",
        persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "eu.bopet.jocadv.gui.ComponentTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_ComponentAction")
@Messages({
    "CTL_ComponentAction=Component",
    "CTL_ComponentTopComponent=Component Window",
    "HINT_ComponentTopComponent=This is a Component window"
})
public class ComponentTopComponent extends TopComponent implements ExplorerManager.Provider {

    private transient ExplorerManager mgr = new ExplorerManager();    

    private final int FPS;
    private final GLJPanel panel;
    private final GLCanvas canvas;
    private final FPSAnimator animator;
    private final GLRenderer glrenderer;
    private final InputHandler inputHandler;
    private final Dimension minimumDimension;
    private final Component component;

    public Component getComponent() {
        return component;
    }

    public ComponentTopComponent() {

        component = Component.getNewComponent();
        this.setDisplayName(component.getName());

        this.FPS = 60;
        this.panel = new GLJPanel();
        this.canvas = new GLCanvas();
        this.animator = new FPSAnimator(canvas, FPS);
        this.glrenderer = new GLRenderer(component);
        this.inputHandler = new InputHandler(glrenderer);
        this.glrenderer.setInputHandler(inputHandler);
        this.minimumDimension = new Dimension(32, 24);

        initComponents();
        initGLComponents();
        setName(Bundle.CTL_ComponentTopComponent());
        setToolTipText(Bundle.HINT_ComponentTopComponent());

        
        associateLookup(ExplorerUtils.createLookup(mgr, getActionMap()));
        mgr.setRootContext(Node.EMPTY);
        mgr.getRootContext().setDisplayName(component.getName());

        
        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
        System.setProperty("sun.awt.noerasebackground", "true");
        setLayout(new BorderLayout());

        canvas.addGLEventListener(glrenderer);

        canvas.addKeyListener(inputHandler);
        canvas.addMouseListener(inputHandler);
        canvas.addMouseMotionListener(inputHandler);
        canvas.addMouseWheelListener(inputHandler);

        canvas.setMinimumSize(minimumDimension);
        panel.setMinimumSize(minimumDimension);

        add(panel, BorderLayout.CENTER);
        panel.add(canvas, BorderLayout.CENTER);
        
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened() {
        animator.start();
    }

    @Override
    public void componentClosed() {
        animator.stop();
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    private void initGLComponents() {
        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
                panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(canvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
                panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(canvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
        );
    }

    @Override
    public ExplorerManager getExplorerManager() {
        return mgr;
    }
}
